//
//  CMoneyOperate.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/1.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "CMoneyOperate.h"

@interface CMoneyOperate ()
@property (nonatomic, strong) NSDecimalNumber *currentMoney;
@property (nonatomic, strong) NSDecimalNumberHandler *roundHandler;
@end

@implementation CMoneyOperate

- (instancetype)init{
    self = [super init];
    if (self) {
        self.round = 2;
    }
    return self;
}

+ (CMoneyOperate *) operateMoney:(NSString *)money{
    CMoneyOperate *operate = [[CMoneyOperate alloc] init];
    operate.currentMoney = [NSDecimalNumber decimalNumberWithString:money];
    return operate;
}

- (CMoneyOperate *(^)(NSString *)) add{
    return ^(NSString *money){
        _currentMoney = [_currentMoney decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:money] withBehavior:_roundHandler];
        return self;
    };
}

- (CMoneyOperate *(^)(NSString *)) del{
    return ^(NSString *money){
        _currentMoney = [_currentMoney decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:money] withBehavior:_roundHandler];
        return self;
    };
}

- (CMoneyOperate *(^)(NSString *)) mul{
    return ^(NSString *money){
        _currentMoney = [_currentMoney decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:money] withBehavior:_roundHandler];
        return self;
    };
}

- (CMoneyOperate *(^)(NSString *)) by{
    return ^(NSString *money){
        _currentMoney = [_currentMoney decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:money] withBehavior:_roundHandler];
        return self;
    };
}

- (BOOL) eq:(NSString *)money{
    NSComparisonResult result = [_currentMoney compare:[NSDecimalNumber decimalNumberWithString:money]];
    return result == NSOrderedSame;
}

- (BOOL) neq:(NSString *)money{
    NSComparisonResult result = [_currentMoney compare:[NSDecimalNumber decimalNumberWithString:money]];
    return result != NSOrderedSame;
}

- (BOOL) gt:(NSString *)money{
    NSComparisonResult result = [_currentMoney compare:[NSDecimalNumber decimalNumberWithString:money]];
    return result == NSOrderedDescending;
}

- (BOOL) ngt:(NSString *)money{
    NSComparisonResult result = [_currentMoney compare:[NSDecimalNumber decimalNumberWithString:money]];
    return result != NSOrderedDescending;
}

- (BOOL) lt:(NSString *)money{
    NSComparisonResult result = [_currentMoney compare:[NSDecimalNumber decimalNumberWithString:money]];
    return result == NSOrderedAscending;
}

- (BOOL) nlt:(NSString *)money{
    NSComparisonResult result = [_currentMoney compare:[NSDecimalNumber decimalNumberWithString:money]];
    return result != NSOrderedAscending;
}

- (NSString *) getValue{
    self.add(@"0");
    NSString *value = [NSString stringWithFormat:@"%@", _currentMoney];
    value = [NSString stringWithFormat:[NSString stringWithFormat:@"%%.%zdf", _round], [value floatValue]];
    return value;
}

- (void)setRound:(NSInteger)round{
    _round = round;
    _roundHandler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers
                                                                           scale:round + 1
                                                                raiseOnExactness:NO
                                                                 raiseOnOverflow:NO
                                                                raiseOnUnderflow:NO
                                                             raiseOnDivideByZero:NO];
    [NSDecimalNumber setDefaultBehavior:_roundHandler];
}

@end
